package com.aistar.service.impl;

import com.aistar.mapper.ChoiceCollectMapper;
import com.aistar.pojo.ChoiceCollect;
import com.aistar.pojo.ChoiceCollectExample;
import com.aistar.service.ChoiceCollectService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChoiceCollectServiceImpl implements ChoiceCollectService {

    @Autowired
    private ChoiceCollectMapper choiceCollectMapper;
    @Override
    public ServerResponse add(ChoiceCollect choiceCollect) {
        int rows=choiceCollectMapper.insert(choiceCollect);
        if(rows>0)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse delete(Integer choiceCollectId) {
        ChoiceCollect choiceCollect=new ChoiceCollect();
        choiceCollect.setChoiceCollectId(choiceCollectId);
        choiceCollect.setStatus(0);
        int rows=choiceCollectMapper.updateByPrimaryKeySelective(choiceCollect);
        if(rows>0)
            return ServerResponse.deleteSuccess();
        return ServerResponse.deleteFailed();
    }


    @Override
    public ServerResponse getById(Integer id) {
        ChoiceCollect choiceCollect=choiceCollectMapper.selectByPrimaryKey(id);
        if(choiceCollect!=null)
            return ServerResponse.getSuccess(choiceCollect);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getAll() {
        ChoiceCollectExample example=new ChoiceCollectExample();

        List<ChoiceCollect> choiceCollectList=choiceCollectMapper.selectByExample(null);
        if(choiceCollectList!=null && choiceCollectList.size()>0)
            return ServerResponse.getSuccess(choiceCollectList);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByCusAndCol(Integer customerId, Integer choiceId) {
        ChoiceCollectExample example=new ChoiceCollectExample();
        example.createCriteria().andCustomerIdEqualTo(customerId)
                .andChoiceIdEqualTo(choiceId);
        List<ChoiceCollect> choiceCollectList=choiceCollectMapper.selectByExample(example);
        if(choiceCollectList !=null && choiceCollectList.size()>0)
            return ServerResponse.getSuccess(choiceCollectList.get(0));
        return ServerResponse.getFailed();
    }
}
